﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

namespace System
{
    /// <summary>
    /// Json帮助类
    /// </summary>
    public class JsonHelper
    {
        static JsonHelper()
        {
            JsonConvert.DefaultSettings = GetSetting;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        private static JsonSerializerSettings GetSetting()
        {
            var result = new JsonSerializerSettings();
            result.Error = OnError;
            return result;
        }

        /// <summary>
        /// 错误
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void OnError(object sender, ErrorEventArgs e)
        {
            e.ErrorContext.Handled = true;
        }

        /// <summary>
        /// 反序列化json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T DeSerialize<T>(string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }

        /// <summary>
        /// 序列号json
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Serialize(object data)
        {
            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 反序列号json,并放进对象内
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <param name="anonymouseTypObj"></param>
        /// <returns></returns>
        public static T DeSerialize<T>(string json, T anonymouseTypObj)
        {
            try
            {
                return JsonConvert.DeserializeAnonymousType(json, anonymouseTypObj);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 根据key获取value
        /// </summary>
        /// <param name="objinfo"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValueByKey(string objinfo, string key)
        {
            string strValue = string.Empty;

            var obj = JObject.Parse(objinfo);
            if (obj.Property(key) == null)
                return "";
            strValue = obj[key].ToString();

            return strValue;
        }

        /// <summary>
        /// 根据key获取value
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValueByKey(JObject obj, string key)
        {
            string strValue = string.Empty;

            #region Newtonsoft.Json
            //             var obj = JObject.Parse(objinfo);
            //strValue = obj[key].ToString();
            var cont = obj.GetValue(key);
            if (cont != null)
            {
                strValue = cont.ToString();
            }
            #endregion;

            return strValue;
        }

        /// <summary>
        /// 根据key获取value(集合)
        /// </summary>
        /// <param name="objinfo"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static List<string> GetArrayByKey(string objinfo, string key)
        {
            List<string> array = new List<string>();

            var obj = JObject.Parse(objinfo);
            var cont = obj.GetValue(key);
            //var cont = obj[key];
            //for (int i = 0; i < cont.; i++)
            //{
            //    //array.Add(obj[i].ToString());
            //}
            if (cont != null && cont.Type == JTokenType.Array)
            {
                JToken t = cont.First;
                while (t != null)
                {
                    array.Add(t.ToString());
                    t = t.Next;
                }
            }

            return array;
        }

        /// <summary>
        /// 根据key获取value(int)
        /// </summary>
        /// <param name="objInfo"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetIntValueByKey(string objInfo, string key)
        {
            int value;

            var obj = JObject.Parse(objInfo);
            value = int.Parse(obj[key].ToString());

            return value;
        }

        /// <summary>
        /// 根据key获取value(int)
        /// </summary>
        /// <param name="objInfo"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetSDKIntValueByKeyEx(JObject objInfo, string key)
        {
            string strValue = string.Empty;
            var cont = objInfo.GetValue(key);
            if (cont != null)
            {
                strValue = cont.ToString();
            }
            else
                return 0;
            return int.Parse(strValue);
        }
    }
}
